Could not autowire. No beans of ‘UserMapper‘ type found.

您所在的位置:网站首页 bean milk 翻译 Could not autowire. No beans of ‘UserMapper‘ type found.

Could not autowire. No beans of ‘UserMapper‘ type found.

2023-08-18 01:09| 来源: 网络整理| 查看: 265

文章来自

SpringBoot与MyBatis整合的时候,也遇到过这个问题。

原因可能有两个,

第一个是IntellijIDEA本身工具的问题。

第二个便是我们导入@Service包的时候导入包错误造成的

检查完第二个问题后,没有发现问题,那就是idea的问题, 问题产生的原因:因为 @Mapper 这个注解是 Mybatis 提供的,而 @Autowried 注解是 Spring 提供的,IDEA能理解 Spring 的上下文,但是却和 Mybatis 关联不上。而且我们可以根据 @Autowried 源码看到,默认情况下,@Autowried 要求依赖对象必须存在,那么此时 IDEA 只能给个红色警告了。

解决方案: 方案一 在自动转配的注解后面添加(required=false) 当我们在使用@Autowired注解的时候,默认required=true,表示注入的时候bean必须存在,否则注入失败。

@Autowired(required=false) public UserMapper userMapper;

方案二 更改idea的设置 在这里插入图片描述 方案三 在UserMapper上面添加 @Component(value =“userMapper”)或者@Repository(value =“userMapper”)

@Repository public interface UserMapper extends BaseMapper { }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3